package com.lzh.others;

import cn.hutool.core.thread.ExecutorBuilder;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

public class SingleExecutor {
    private static final ExecutorService SINGLE_EXECUTOR = ExecutorBuilder.create()
            .setCorePoolSize(1)
            .setMaxPoolSize(1)
            .setWorkQueue(new LinkedBlockingQueue<>(1))
            .setHandler(new ThreadPoolExecutor.DiscardOldestPolicy())
            .build();

    public static void main(String[] args) {
        System.out.println("这是主线程");
        System.out.println("当前线程：" + Thread.currentThread().getName());
        System.out.println("当前线程：" + Thread.currentThread().getId());
        System.out.println("当前线程：" + Thread.currentThread().getStackTrace());
        System.out.println("当前线程：" + Thread.currentThread().getStackTrace()[0].getClassName());
        System.out.println("当前线程：" + Thread.currentThread().getStackTrace()[0].getMethodName());

        SINGLE_EXECUTOR.execute(() -> {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("这是子线程1：" + Thread.currentThread().getName());
                    System.out.println("当前线程：" + Thread.currentThread().getId());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace()[0].getClassName());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace()[0].getMethodName());
                }
        );
        SINGLE_EXECUTOR.execute(() -> {
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("这是子线程2：" + Thread.currentThread().getName());
                    System.out.println("当前线程：" + Thread.currentThread().getId());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace()[0].getClassName());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace()[0].getMethodName());
                }
        );
        SINGLE_EXECUTOR.execute(() -> {
                    try {
                        Thread.sleep(4500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("这是子线程3：" + Thread.currentThread().getName());
                    System.out.println("当前线程：" + Thread.currentThread().getId());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace()[0].getClassName());
                    System.out.println("当前线程：" + Thread.currentThread().getStackTrace()[0].getMethodName());
                }
        );
    }
}
